/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> generateTrees(int n) {
        if (!n)
            return{};
        return gen(1, n);
    }
    vector<TreeNode*> gen(int i, int j) {
        // if(i == j) return {new TreeNode(i)};
        vector<TreeNode*> result;
        if(i > j) result.push_back(NULL);
        for(int k = i; k <= j; k++)
        {
            for(auto n : gen(i, k - 1))
                for(auto m : gen(k + 1, j))
                {
                    TreeNode* t = new TreeNode(k);
                    t -> left = n;
                    t -> right = m;
                    result.push_back(t);
                }
        }
        return result;
    }
};
